$(document).ready(function(){
	$.ajax({
		type:"get",
		url:'../DailySentimentServlet?method=sentmentHistory',
		data:{
			"pagenum":1
		},
		success:function(data){
			console.log(data);
			var recordsTotal = JSON.parse(data).count;
		    console.log("recordsTotal:"+recordsTotal);
		    if(recordsTotal != 0){
			   appendHistoryRecordResult(data);
			   var url="../DailySentimentServlet?method=sentmentHistory";
			   pageMaker(recordsTotal,url,"pagetoolBar","historyRecordDaily");
		    }else{
		    	$("#historyRecordDailyTable").append("<h4>无历史记录信息</h4>");
		    }
		   
		},
		error:function(){
			console.log("error");
		}
	})
})

// 简单方法提炼出文章概要，取前100个字
function summary(text) {
	var str = "";
	var i = 0;
	while (str.length <= 100) {
		str += text[i++];
	}
	console.log(str);
	return str;
}
//删除按钮方法
function deleteDailyRecord(record_id){
	$.ajax({
		type:"get",
		url:'../DailySentimentServlet?method=deleteReport',
		data:{
			"checkarray":record_id
		},
		success:function(data){
			location.reload();
		},
		error:function(){
			console.log("error");
		}
	});
}
//翻页方法
function historyRecordDaily(data){
	$("tbody").remove();
	appendPermissionResult(data);
}
//有数据的情况下append数据到结果到页面，生成表格
function appendHistoryRecordResult(data){
	 var historyRecord = JSON.parse(data).reports;
	    $.each(historyRecord,function(indexs,values){
//		 		
	    	var tr = "<tr><td>"+ this.title+ "</td><td>"+ summary(this.text)+ "</td><td>"+this.date+"</td>";
         var btn ="<td class='center'>"
              +"<button id='"+this._id.$oid+"_delete' class='btn btn-danger' >"
              +"<i class='glyphicon glyphicon-trash icon-white'></i>删除</button></td></tr>";  
			tr +=btn;

			// 遍历添加行
			$("#historyRecordDailyTable").append(tr);
//			绑定删除按钮点击事件
	    	var btndelete_id =this._id.$oid+"_delete";
	    	$("#"+btndelete_id).click(function(){
		    		var record_id = this.id.split("_");
		    		deleteDailyRecord(record_id[0]);
	    	});   
	   
	    });
}